package com.bank.accountinfo.account.service.impl;

import com.bank.accountinfo.account.dao.AccountDao;
import com.bank.accountinfo.account.entity.Account;
import com.bank.accountinfo.account.service.AccountService;
import com.bank.trade_records.cun_money.service.CunMoneyService;
import com.bank.trade_records.qu_money.service.QuMoneyService;
import com.bank.trade_records.zhuan_money.service.ZhuanMoneyService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;
    @Autowired
    private CunMoneyService cunMoneyService;
    @Autowired
    private QuMoneyService quMoneyService;
    @Autowired
    private ZhuanMoneyService zhuanMoneyService;

    @Override
    public Account getAccountById(Long id) {
        return accountDao.selectById(id);
    }

    @Override
    @Transactional
    public boolean addAccount(Account account) {
        return accountDao.insert(account) > 0;
    }

    @Override
    @Transactional
    public boolean updateAccount(Account account) {
        return accountDao.updateById(account) > 0;
    }

    @Override
    @Transactional
    public boolean deleteAccountById(Long id) {
        return accountDao.deleteById(id) > 0;
    }

    @Override
    public List<Account> getAllAccounts() {
        return accountDao.selectList(null);
    }

    @Override
    @Transactional
    public boolean batchInsert(List<Account> accountList) {
        return accountDao.insertBatchSomeColumn(accountList) > 0; // MyBatis-Plus提供了批量插入的方法
    }

    @Override
    public Page<Account> getAccountPage(Page<Account> accountPage) {
        return accountDao.selectPage(accountPage, null);
    }

    @Override
    public List<List<Object>> getTransactionSummary(){
        LocalDate today = LocalDate.now();
        List<LocalDate> lastFiveDays = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            lastFiveDays.add(today.minusDays(i));
        }

        // 构建表头
        List<List<Object>> result = new ArrayList<>();
        List<Object> headers = new ArrayList<>();
        headers.add("表名");
        for (LocalDate date : lastFiveDays) {
            headers.add(date.toString());
        }
        result.add(headers);
        //存入交易记录数据
        List<Object> cunMoney=cunMoneyService.getDailyTransactionAmounts();
        List<Object> quMoney=quMoneyService.getDailyTransactionAmounts();
        List<Object> zhuanMoney=zhuanMoneyService.getDailyTransactionAmounts();
        result.add(cunMoney);
        result.add(quMoney);
        result.add(zhuanMoney);
        return result;
    }
    @Override
    public List<List<Object>> getBalanceListByAccountId(String accountId) {
        // 执行SQL查询获取实际数据
        List<Map<String, Object>> daoResult=  accountDao.getBalanceListByAccountId(accountId);
        List<Object> dates = new ArrayList<>();
        List<Object> balance = new ArrayList<>();

        for (Map<String, Object> map : daoResult){
            dates.add(map.get("date"));
            balance.add(map.get("balance"));
        }
        List<List<Object>> result = new ArrayList<>();
        result.add(dates);
        result.add(balance);
        return result;
        // 将查询结果转换为以日期为键的Map

}

}